#include <iostream>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <algorithm>
using std::vector;
using std::cout;

int count;

void dumpnums(vector<int> &v,vector<int> &visited){
	if(visited.size()==v.size()){
		std::copy(visited.begin(),visited.end(),std::ostream_iterator<int>(cout," "));
		cout<<"\n";
		count++;
		return;
	}
	
	for(int i=0;i<v.size();i++){
		int s=v[i];
		vector<int>::const_iterator it=std::find(visited.begin(),visited.end(),s);
		if(it == visited.end()){
//			cout<<"push back :"<<v[i]<<" and ";
//			std::copy(visited.begin(),visited.end(),std::ostream_iterator<int>(cout," "));
//			cout<<"\n";
			
			visited.push_back(v[i]);
			dumpnums(v,visited);
			vector<int>::iterator nt=std::find(visited.begin(),visited.end(),s);
			visited.erase(nt);
		}
	}
}

int main(){
	
	
	vector<int> v;
	for(int i=5;i>0;i--){
		v.push_back(i);
	}
	std::copy(v.begin(),v.end(),std::ostream_iterator<int>(cout," "));
	
	vector<int> visited;
	dumpnums(v,visited);
	
	cout<<"\n"<<"total:"<<count<<"\n";
}